package com.xianbeiying.app.ckgl.activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.adapter.SuperViewPagerAdapter;
import com.xianbeiying.app.ckgl.util.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;
/**
 * 欢迎界面
 */
public class WelcomeActivity extends BaseActivity{
	private ViewPager introductVp; // 介绍页面
	private SuperViewPagerAdapter viewPagerAdapter;
	private List<View> views = new ArrayList<View>(); // Tab页面列表
	// 底部小点图片
	private ImageView[] dots;
	// 记录当前选中位置
	private int currentIndex;
	LinearLayout ll;
//	private TimeCount time;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		isPermisson=true;
//		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//				WindowManager.LayoutParams.FLAG_FULLSCREEN);
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.guide);
		//time = new TimeCount(3000, 1000);// 构造CountDownTimer对象
		initView();
		initDots();
	}
	private void initDots() {
		ll = (LinearLayout) findViewById(R.id.ll);
		ll.setVisibility(View.VISIBLE);
		dots = new ImageView[views.size()];
		// 循环取得小点图片
		for (int i = 0; i < views.size(); i++){
			dots[i] = (ImageView) ll.getChildAt(i);
			dots[i].setEnabled(true);// 都设为灰色
		}
		currentIndex = 0;
		dots[currentIndex].setEnabled(false);// 设置为白色，即选中状态
	}

	private void initView() {
		AssetManager mgr = this.getAssets();// 得到AssetManager
		introductVp = (ViewPager) findViewById(R.id.viewpager);
		introductVp.setOffscreenPageLimit(4);
		View view1 = View.inflate(this, R.layout.fragmnet_guide1, null);
		View view2 = View.inflate(this, R.layout.fragmnet_guide2, null);
		View view3 = View.inflate(this, R.layout.fragmnet_guide3, null);
		View view4 = View.inflate(this, R.layout.fragmnet_guide4, null);
		views.add(view1);
		views.add(view2);
		views.add(view3);
		views.add(view4);
		viewPagerAdapter = new SuperViewPagerAdapter(views);
		introductVp.setAdapter(viewPagerAdapter);
		introductVp.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {
				// 设置底部小点选中状态
				setCurrentDot(arg0);
				if(arg0==3){
					//进行3s跳转
					goHome();
				}

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}

	private void setCurrentDot(int position) {
		if (position < 0 || position > views.size() - 1
				|| currentIndex == position) {
			return;
		}
//		if (position == 3) {
//			ll.setVisibility(View.GONE);
//		} else {
			ll.setVisibility(View.VISIBLE);
//		}
		dots[position].setEnabled(false);
		dots[currentIndex].setEnabled(true);
		currentIndex = position;
	}
	private AlphaAnimation animation;
	/**
	 * 淡入淡出开始动画
	 */
	private void startAlphaAnimation(View textShow) {
		if (animation == null) {
			// 创建一个AlphaAnimation对象
			animation = new AlphaAnimation(0.01f, 1f);
			// 设置动画执行的时间（单位：毫秒）
			animation.setDuration(800);
		}
		// 把动画设置给控件
		textShow.setAnimation(animation);
		// 开始动画
		animation.start();
	}
	/**
	 * 结束动画
	 */
	private void cancelAlphaAnimation() {
		if (animation != null) {
			animation.cancel();
		}
	}
	private void goHome(){
		new Handler().postDelayed(new Runnable(){
			public void run() {
				// 跳转
				SharedPreferencesUtils.setBooleanDate("isShow",true);
				Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
				startActivity(intent);
				finish();
			}
		}, 2000);
	}

//	public void onTclick(View v){
//		// 设置第一次进入参数变化
////		SharedpreferncesUtil.FRISTMessage(this, "isFirstIn", false);
//		goHome();
//	}

}
